var program =  {
		getclassrooms_list : function() {
		$.ajax({
				type : "GET",
				url : "http://127.0.0.1:8080/yl-websocket/transmit?param='m=assgetcr&address=http://103.37.166.4:7777/StateGrid/cra.do",
				scriptCharset : 'utf-8',
				success : function(param) {
//					alert(param)
					param = JSON.parse(param).data;
					program.createTable(param);
					program.list = param;
				},
				error : function() {
				}
			});
		},
		createTable : function(params){
			$("#getclassrooms_list").html("");
			var str = "";
			var count = 0;
			var j = 0;
			for ( var i in params) {
				if(params[i].alive=="1"){
					params[i].alive = "开启";
				}else if(params[i].alive=="0"){
					params[i].alive = "关闭";
				}
				if(params[i].isDel!="1"){
					j ++;
					var param = JSON.stringify(params[i]).replace(new RegExp('"','gm'),'|');
					if(i==0){
						str += '<tr class="active" id="tr'+j+'" onclick="createContent(\''+param+'\',\'tr'+j+'\')")"><td class="right">'+j+'</td><td colspan="8"><span class="havaBg"></span>'+params[i].ecdClassRoomName+'</td><td class="right green">'+params[i].alive+'</td></tr>';
						program.selectedRoom = params[i].ecdClassRoomId;
						program.selectedContent = params[i];
						createContent(param,'tr'+j);
					}else{
						str += '<tr id="tr'+j+'" onclick="createContent(\''+param+'\',\'tr'+j+'\')")"><td class="right">'+j+'</td><td colspan="8"><span class="havaBg"></span>'+params[i].ecdClassRoomName+'</td><td class="right green">'+params[i].alive+'</td></tr>';
					}
				}
			}
			$("#count").html(j);
			$(str).appendTo("#getclassrooms_list");
		},
		searchroom : function (){
			var condition = $("#selectCondition").val();
			var condition_str = $("#condition_str").val();
			if(condition_str == ""){
				program.createTable(program.list);
			}
			var new_list = program.mapping(condition,condition_str);
			program.createTable(new_list);
		},
		mapping : function (condition,condition_str){
			var list = [];
			j = 0;
			if(condition == "1"){
				for(var i in program.list){
					if(program.list[i].ecdClassRoomName.indexOf(condition_str)>-1){
						list[j] = program.list[i];
						j++;
					}
				}
			}else if(condition == "2"){
				for(var i in program.list){
					if(program.list[i].ecdClassRoomDescription.indexOf(condition_str)>-1){
						list[j] = program.list[i];
						j++;
					}
				}
			}
			return list;
		},
		selectedId : "tr1",
		selectedRoom : "",
		selectedContent: {},
		list : {}
	}

//查询演练室的所有事件

var getevents = {
	getevents_list : function() {
		console.info(program.selectedContent);
		$("#ecdName").html(program.selectedContent.ecdClassRoomName);
		$("#protName").html(program.selectedContent.projectName);
		$("#ecdDescription").html(program.selectedContent.ecdClassRoomDescription);
//		var userJson = JSON.parse(sessionStorage.getItem("suid")).data;
//		$("#login_name2").html(userJson.su_name);
		var crid = sessionStorage.getItem("crid");
		$.ajax({
					type : "GET",
					url : "http://127.0.0.1:8080/yl-websocket/transmit?param='m=getevents,crid="+ crid +"&address=http://103.37.166.4:7777/StateGrid/cra.do",
					async : false,
					scriptCharset : 'utf-8',
					success : function(param) {
						param = JSON.parse(param).data;
						getevents.allSj = param;
						getevents.createTable(param);
//						alert(param);
						
					},
					error : function() {
					}
				});
	},
	allSj : {},
	getUsers : function(eventid){
		$.ajax({
			type : "GET",
			url : "http://127.0.0.1:8080/yl-websocket/transmit?param='m=eventusers,eventid="+ eventid +"&address=http://103.37.166.4:7777/StateGrid/ua.do",
			async : false,
			scriptCharset : 'utf-8',
			success : function(param) {
//				alert(param);
//				sessionStorage.setItem("parames", param);
				getevents.allUsers = JSON.parse(param).data;
//				getevents.createTable()
			},
			error : function() {
			}
		});
	},
	allUsers : {},
	createTable : function(params) {
		$("#getevents_list").html("");
		var str = "";
		var j = 0;
		var n = 0;
		for ( var i in params) {
			j++;
			str += '<div style="max-height: 162px;overflow-y: scroll;"><table class="Ptable" id="">'

			var s1 = "#a0a0ff";
			var s2 = "#ff9900";
			var s3 = "#ff3300";
			var s4 = "#0070c0";
			var s5 = "#ff00cc";
			var colors = [ {
				"color" : "#a0a0ff"
			}, {
				"color" : "#ff9900"
			}, {
				"color" : "#ff3300"
			}, {
				"color" : "#0070c0"
			}, {
				"color" : "#ff00cc"
			}, ]

			j = 0;
			getevents.getUsers(params[i].ceventId); //fenbie
			
			for ( var k in params[i].ecdClassRoomEventPostList) {
				j++;
				n--
				var h = params[i].ecdClassRoomEventPostList.length + 1;
				var userName;
				for(var ii in getevents.allUsers){
					if(params[i].ecdClassRoomEventPostList[k].ecdrepId == getevents.allUsers[ii].ecdrepId){
						userName = getevents.allUsers[ii].realizeUsers[0].su_name;
					}
				}
				if (k == 0) {
					str += '<tr><td rowspan="'
							+ h
							+ '" style="background:'+colors[i].color+';width: 1px;" class="'
							+ 's'
							+ j
							+ '"></td><td colspan="3" rowspan="'
							+ h
							+ '" style="width: 40px;" valign="top">'
							+ params[i].ceventName
							+ '</td><td colspan="3">【角色'
							+ j
							+ '】<span class="geren"></span>'
							+ params[i].ecdClassRoomEventPostList[k].ecdrepName
							+ '</td><td colspan="2">【用户】'
							+ userName
							+ '</td><td><a href="javascript:;" class="addUser" onclick="editUser(\''+ params[i].ecdClassRoomEventPostList[k].ownerClassRoomEventId + '\',\''+ params[i].ecdClassRoomEventPostList[k].ecdrepId + '\',\''+ params[i].ecdClassRoomEventPostList[k].ecdrepName + '\',\''+ params[i].ceventName + '\')" id="assess"></a></td></tr>';
				} else {               
					str += '<tr><td colspan="3">【角色'
							+ j
							+ '】<span class="geren"></span>'
							+ params[i].ecdClassRoomEventPostList[k].ecdrepName
							+ '</td><td colspan="2">【用户】'
							+ userName
							+ '</td><td><a href="javascript:;" class="addUser" onclick="editUser(\''+ params[i].ecdClassRoomEventPostList[k].ownerClassRoomEventId + '\',\''+ params[i].ecdClassRoomEventPostList[k].ecdrepId + '\',\''+ params[i].ecdClassRoomEventPostList[k].ecdrepName + '\',\''+ params[i].ceventName + '\')" id="assess"></a></td></tr>';
				}
			}
			str += '</table></div>'
			console.info(params);

		}
		$(str).appendTo("#getevents_list");
		// $(str).appendTo("#getevents_pros")
	}

}
//查看角色演练情况与评分
var gettarmsg = {
		gettarmsg_list : function(eventid,pid,rolName){
			$("#rolName").html(rolName);
			console.info(eventid,pid);
			$.ajax({
				type : "GET",
				url : "http://127.0.0.1:8080/yl-websocket/transmit?param='m=gettarmsg,eventid="+ eventid +",pid="+ pid +"&address=http://103.37.166.4:7777/StateGrid/ass.do",
				async : false,
				scriptCharset : 'utf-8',
				success : function(param) {
//					alert(param);
					gettarmsg.param = JSON.parse(param).data;
					param = JSON.parse(param).data;
					gettarmsg.gettarmsg_select(param);
				},
				error : function() {
				}
			});
		},
		param : "",
		//消息接收与查看情况
		gettarmsg_select : function(param){
			console.info(param)
			$("#xiaoxi_info").html("");
			var ridey = "";
			var str = "";
			str +='<tr><th>序号</th><th>消息编号</th><th>发送者</th><th>接受者</th><th>消息内容</th><th>发送时间</th><th>查看详情</th><th>是否已读</th></tr>'
			var j = 0;
			var n = 0;
			for(var i in param){
				j++;
				if(param[i].ecdEventMessageIsReaded == 0){
					ridey = "No01";
				}else if(param[i].ecdEventMessageIsReaded == 1){
					n++;
					ridey = "yes01";
				}
				var param1 = JSON.stringify(param[i]).replace(new RegExp('"','gm'),'|');
				str +='<tr><td>'+j+'</td><td>'+param[i].ecdEventMessageId+'</td><td>'+param[i].ecdEventMessageSender+'</td><td>'+param[i].messageReceiverName+'</td><td title="'+param[i].ecdEventMessageContent+'">'+param[i].ecdEventMessageContent.substring(0,30)+'</td><td>'+param[i].ecdEventMessageSendTime+'</td><td><span onclick="xin_info(\''+i+'\')" class="operation" id="ppop"></span></td><td><span class="'+ ridey +'"></span></td></tr>'
			}
			
//			<tr><td>1</td><td>【安质部】【6-1】向国网公司总部以及政府部门</td><td>地震大面积修电</td><td>无</td><td>editor01</td><td>editor01</td><td><span class="operation" id="ppop"></span></td><td><span class="yes01"></span></td></tr>
			
			$(str).appendTo("#xiaoxi_info");
			$("#yridy").html(n);
			$("#total").html(j);
		},
		refresh : function(){
			if(questions.refreshState == "1"){
				getevents.getevents_list();
			}else if(questions.refreshState == "2"){
				getevents.userList();
			}else if(questions.refreshState == "3"){
				getevents.getaction_select()
			}else{
				
			}
		},
		//获取角色;
		userList : function(){
			var eventid = sessionStorage.getItem("eventid");
			$.ajax({
				type : "GET",
				url : "http://127.0.0.1:8080/yl-websocket/transmit?param='m=eventusers,eventid="+ eventid +"&address=http://103.37.166.4:7777/StateGrid/ua.do",
				async : false,
				scriptCharset : 'utf-8',
				success : function(param) {
//					alert(param);
					param = JSON.parse(param).data;
					gettarmsg.getroleques(param);
				},
				error : function() {
				}
			});
		},
		//研讨情况
		getroleques : function(param){
			var suid = "";
			var su_name = "";
			var eventid = sessionStorage.getItem("eventid");
			var pid = sessionStorage.getItem("pid");
			var crid = sessionStorage.getItem("crid");
			for(var i in param){
				var ecdrepId = param[i].ecdrepId
				if(ecdrepId == pid){
					 suid = param[i].realizeUsers[0].su_id;
					 su_name = param[i].realizeUsers[0].su_name;
				}
			}
			$.ajax({
				type : "GET",
				url : "http://127.0.0.1:8080/yl-websocket/transmit?param='m=getroleques,crid="+ crid +",eventid="+ eventid +",suid="+ suid +"&address=http://103.37.166.4:7777/StateGrid/crqu.do",
				scriptCharset : 'utf-8',
				success : function(param) {
//					alert(param)
					param = JSON.parse(param).data;
					gettarmsg.getroleques_listyt(param);
//					alert(param);
					
				},
				error : function() {
				}
			});
			$("#su_name").html(su_name);
			var suid_id = suid;
			sessionStorage.setItem("suid_id", suid_id);
		},
		getroleques_listyt : function(param){
			$("#yantao_info").html("");
			var str = "";
			str += '<tr><th>序号</th><th>研讨题目</th><th>讨论结果</th><th>参考结果</th><th>提交时间</th></tr>';
			var j = 0;
			var n = 0;
			for(var i in param){
				j++;
				if(param[i].questionLogAnswerContent==" "||param[i].questionLogAnswerContent==""||param[i].questionLogAnswerContent==null){
					
				}else if(param[i].questionLogAnswerContent!=" "||param[i].questionLogAnswerContent==""||param[i].questionLogAnswerContent==nullparam[i].questionLogAnswerContent!=""||param[i].questionLogAnswerContent!=null){
					n++;
				}else{
					
				}
				if(param[i].questionInfo == "" || param[i].questionInfo == null){//判断缺少的时候进行添加一个数组;
					param[i].questionInfo = {};
					param[i].questionInfo.questionText = "";
					param[i].questionInfo.questionAnswer = "";
					
				}
				str +='<tr><td>'+j+'</td><td title="'+param[i].questionInfo.questionText+'" >'+ param[i].questionInfo.questionText.substring(0,25)+'</td><td title="'+param[i].questionLogAnswerContent+'">'+param[i].questionLogAnswerContent.substring(0,25)+'</td><td title="'+param[i].questionInfo.questionAnswer+'">'+ param[i].questionInfo.questionAnswer.substring(0,25)+'</td><td>'+ param[i].questionLogAnswerTime+'</td></tr>'
			}
			$(str).appendTo("#yantao_info");
			$("#ytridy").html(n);
			$("#yt_total").html(j);
			
		},
		//行动策划获取值
		getaction_select : function(){
			var crid = sessionStorage.getItem("crid");
			var eventid = sessionStorage.getItem("eventid");
			$.ajax({
				type : "GET",
				url : "http://127.0.0.1:8080/yl-websocket/transmit?param='m=getaction,eventid=" + eventid +",crid=" + crid +"&address=http://103.37.166.4:7777/StateGrid/fact.do",
				scriptCharset: 'utf-8', 
				success : function(param) {
//					alert(param);
					param = JSON.parse(param);
					gettarmsg.shcemeList(param.data);
				},
				error: function(){
				}
			});
		},
		//行动策划
		shcemeList : function(param){
			$("#shceme").html("");
			var str = "";
			var j = 0;
			str = '<tr><th>序号</th><th>任务内容</th><th>行动要点</th><th>执行时间</th></tr>';
			for(var i in param){
				j++;
				if(param[i].relatedCases == null){
					param[i].relatedCases = {};
					param[i].relatedCases.caseContent = "";
				}
				str += '<tr><td>'+j+'</td><td title="'+param[i].relatedCases.caseContent+'" >'+param[i].relatedCases.caseContent.substring(0,20)+'</td><td title="'+ param[i].actionContents +'">'+param[i].actionContents.substring(0,20)+'</td><td>'+param[i].actionTime+'</td></tr>';
			}
			$("#un_shulian").html(j);
			$(str).appendTo("#shceme");
		},
		refreshState : "1",
}

//评估提交
var  ass = {
		ass_Tj : function(){
			var text = $("#pingyu").val();
			var score = $("#pingfen").val();
			var crid = sessionStorage.getItem("crid");
			var eventid = sessionStorage.getItem("eventid");
			var pid = sessionStorage.getItem("pid");
			var suid = sessionStorage.getItem("suid_id");
			var rolName = sessionStorage.getItem("rolName");
			var sjName = sessionStorage.getItem("sjName");
			var tar = sjName + rolName;
			if(getHx.recordId == "" || getHx.recordId ==null){
				$.ajax({
					type : "GET",
					url : "http://127.0.0.1:8080/yl-websocket/transmit?param='m=ass,eventid=" + eventid +",crid=" + crid +",text=" + text +",score=" + score +",pid=" + pid +",suid=" + suid +",tar=" + tar +"&address=http://103.37.166.4:7777/StateGrid/ass.do",
					scriptCharset: 'utf-8', 
					success : function(param) {
//						alert(param);
//						param = param.replace(";","");//将;替换成""(空串);
//						param = JSON.parse(param).data;
//						sessionStorage.setItem("rid", param.recordId);
//						getHx.getAss_hx(param);
//						againXg.again_xg(param);
						
					},
					error: function(){
					}
				});
			}else{
				
				$.ajax({
					type : "GET",
					url : "http://127.0.0.1:8080/yl-websocket/transmit?param='m=ass,eventid=" + eventid +",crid=" + crid +",text=" + text +",score=" + score +",pid=" + pid +",suid=" + suid +",tar=" + tar +",recid=" + getHx.recordId +"&address=http://103.37.166.4:7777/StateGrid/ass.do",
					scriptCharset: 'utf-8', 
					success : function(param) {
//						alert(param);
						param = param.replace(";","");//将;替换成""(空串);
	//					param = JSON.parse(param).data;
	//					sessionStorage.setItem("rid", param.recordId);
	//					getHx.getAss_hx(param);
	//					againXg.again_xg(param);
						
					},
					error: function(){
					}
				});
			}
		},
}

//评估回显
var getHx = {
		ass_hx : function(){
			var pid = sessionStorage.getItem("pid");
			var suid = sessionStorage.getItem("suid_id");
			$.ajax({
				type : "GET",
				url : "http://127.0.0.1:8080/yl-websocket/transmit?param='m=getass,pid=" + pid +",suid=" + suid +"&address=http://103.37.166.4:7777/StateGrid/ass.do",
				scriptCharset: 'utf-8', 
				success : function(param) {
					$("#pingyu").val("");
					$("#pingfen").val("");
//					alert(param);
					if(param == ""){
						getHx.recordId = "";
					}else{
						param = JSON.parse(param);
						if(param.data == null){
							getHx.recordId = "";
						}else{
							getHx.recordId = param.data.recordId;
							getHx.getAss_hx(param.data);
						}
					}
				},
				error: function(){
				}
			});
		},
		getAss_hx : function(param){
			var pingYu = param.assessContent;
			var pingFen = param.assessScore;
			$("#pingyu").val(pingYu);
			$("#pingfen").val(pingFen);
		},
		recordId : ""
}

var x_info ={
		c_info : function(sjId,bh,fs,fpropre,sTime,sTait,nReste){
			var riderys = "";
			var zhangTai = "";
			for(var ii in getevents.allSj){
				if(getevents.allSj[ii].ceventId == sjId){
					userName = getevents.allSj[ii].ceventName;
				}
			}
			$("#sj").html(userName);
			$("#bh").html(bh);
			if(fs==1){
				riderys = "网络通知";
				$("#fs").html(riderys);
			};
			
			$("#sTime").html(sTime);
			
			$("#fpropre").html(fpropre);
			if(sTait == 0){
				zhangTai="未读"
				$("#sTait").html(zhangTai);
			}
			if(sTait ==1){
				zhangTai="已读"
				$("#sTait").html(zhangTai);
			}
			
			$("#nReste").html(nReste);
			
			
			
			
		}
}

function xin_info(i){
	        var sjId = gettarmsg.param[i].ownerEventId;
	        var bh = gettarmsg.param[i].ecdEventMessageId;
	        var fs = gettarmsg.param[i].ecdEventMessageIsSended;
	        var fpropre = gettarmsg.param[i].ecdEventMessageSender;
	        var sTime = gettarmsg.param[i].ecdEventMessageSendTime;
	        var sTait = gettarmsg.param[i].ecdEventMessageIsReaded;
	        var nReste = gettarmsg.param[i].ecdEventMessageContent;
	        var riderys = "";
			var zhangTai = "";
			for(var ii in getevents.allSj){
				if(getevents.allSj[ii].ceventId == sjId){
					userName = getevents.allSj[ii].ceventName;
				}
			}
			$("#sj").html(userName);
			$("#bh").html(bh);
			if(fs==1){
				riderys = "网络通知";
				$("#fs").html(riderys);
			};
			
			$("#sTime").html(sTime);
			
			$("#fpropre").html(fpropre);
			if(sTait == 0){
				zhangTai="未读"
				$("#sTait").html(zhangTai);
			}
			if(sTait ==1){
				zhangTai="已读"
				$("#sTait").html(zhangTai);
			}
			
			$("#nReste").html(nReste);
//	x_info.c_info(sjId,bh,fs,fpropre,sTime,sTait,nReste);
	$(".ppop").show(500);
	$("body").addClass("overHiden");
}



function  createContent(param,id){
	$("#"+program.selectedId).removeClass('active');
	$("#"+id).addClass('active');
	program.selectedId = id;
	param = replaceAll(param);
	param = JSON.parse(param);
	program.selectedContent = param;
	program.selectedRoom = param.ecdClassRoomId;
	sessionStorage.setItem("crid",param.ecdClassRoomId);
	//获取演练室的信息
	$("#ecdClassRoomName").val(param.ecdClassRoomName);
	$("#projectName").val(param.projectName);
	$("#ecdClassRoomDescription").html(param.ecdClassRoomDescription);
}

$(function() {
	program.getclassrooms_list();
	$("#search").click(function(){
		program.searchroom();
	});
	$("#detail").click(function(){
		getevents.getevents_list();
	});
	
	$(".addPop .close").click(function() {
		if(!$(this).attr("data-type")){
			$(".addPop").hide(500);
			$("body").removeClass("overHiden");
			location.reload(); 
		}
	});
	//资源判断
	$("#tab1").click(function(){
		gettarmsg.refreshState = "1";
	});
	$("#tab2").click(function(){
		gettarmsg.refreshState = "2";
	});
	$("#tab3").click(function(){
		gettarmsg.refreshState = "3";
	});
	$("#tab4").click(function(){
		gettarmsg.refreshState = "4";
	});
	
	//评估保存
	$("#query_Qd").click(function(){
		ass.ass_Tj();
	});
	
	
});
function editUser(eventid,pid,rolName,sjName){
	$(".assessPop").show(500);
	$("body").addClass("overHiden");
	gettarmsg.gettarmsg_list(eventid,pid,rolName);
	sessionStorage.setItem("pid", pid);
	sessionStorage.setItem("eventid", eventid);
	sessionStorage.setItem("sjName", sjName);
	sessionStorage.setItem("rolName", rolName);
	gettarmsg.userList();
	gettarmsg.getaction_select();
	getHx.ass_hx();
};










